package com.sjyl.kaoshitong.util;

import android.content.Context;

import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * Created by ChangerLow on 15/6/22.
 * Email: changerlow@qq.com
 */
public class OKHttpUtil {
    //private OKHttpClient okHttpClient;
    private static long TIMEOUT = 10;
    private static OkHttpClient okHttpClient;
    private final static int cacheSize = 10*1024*1024;

    public static void init(Context context){
        okHttpClient = new OkHttpClient();
        context.getCacheDir().getPath();
        Cache cache = null;
        File cacheDir = new File(context.getExternalCacheDir().getPath(), "HttpCache");
        cache = new Cache(cacheDir, cacheSize);
        okHttpClient.setCache(cache);
        okHttpClient.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
    }

   public static OkHttpClient getOkHttpClient(Context context){
       if (okHttpClient == null){
           okHttpClient = new OkHttpClient();
           context.getCacheDir().getPath();
           Cache cache = null;
           File cacheDir = new File(context.getExternalCacheDir().getPath(), "HttpCache");
           cache = new Cache(cacheDir, 10 * 1024 * 1024);
           okHttpClient.setCache(cache);
           okHttpClient.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
           okHttpClient.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
       }
       return okHttpClient;
   }

//    public static Response execute(Request request) throws IOException {
//        return okHttpClient.newCall(request).execute();
//    }
//
//    public static void enqueue(Request request, Callback responseCallback){
//        okHttpClient.newCall(request).enqueue(responseCallback);
//    }
}
